package com.example.personalcenter;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import java.util.ArrayList;
import java.util.List;

public class InterestSelectActivity extends AppCompatActivity {
    private CheckBox cbReading;
    private CheckBox cbPhotography;
    private CheckBox cbPingpong;
    private Button btnBack;
    private Button btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_select);

        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingpong = findViewById(R.id.cb_pingpong);
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);

        // 返回按钮点击事件
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 确定按钮点击事件，返回选择的兴趣
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> selectedInterests = new ArrayList<>();

                if (cbReading.isChecked()) {
                    selectedInterests.add(cbReading.getText().toString());
                }
                if (cbPhotography.isChecked()) {
                    selectedInterests.add(cbPhotography.getText().toString());
                }
                if (cbPingpong.isChecked()) {
                    selectedInterests.add(cbPingpong.getText().toString());
                }

                // 将选择的兴趣拼接成字符串
                String interestStr = "";
                for (int i = 0; i < selectedInterests.size(); i++) {
                    if (i > 0) {
                        interestStr += "、";
                    }
                    interestStr += selectedInterests.get(i);
                }

                // 返回结果给上一个页面
                Intent intent = new Intent();
                intent.putExtra("interest", interestStr);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}
